class GameMap {
    data = []
    rows = 0
    columns = 0
    offset = 0
    /**
     * @param {*} x 列数
     * @param {*} y 行数
     * @param {*} picNum 图片总类数
     * @param {*} count 每种图片有几组
     */
    constructor(x, y, offset, picNum, count) {
        if (!x || !y) throw new Error('未指定地图参数')
        this.rows = y
        this.columns = x
        this.offset = offset
        this.initMap()
        const mapData = randomMapData(this.rows, this.columns, picNum, count)
        console.log(mapData)
        mapData.forEach(item => {
            this.data[item.x][item.y].type = item.img
        })
    }
    initMap() {
        for (let i = 0; i < this.rows; i++) {
            this.data.push([])
            for (let j = 0; j < this.columns; j++) {
                this.data[i][j] = {
                    type: null
                }
            }
        }
    }
    reset() {
        this.data.forEach(row => {
            row.forEach(column => {
                column = 0
            })
        })
        this.draw()
    }
    update({ indexX, indexY }, value) {
        this.data[indexX][indexY].type = value
        this.draw()
    }
    isEmptyMap() {
        let isEmpty = true
        this.data.forEach(row => {
            row.forEach(column => {
                if (column !== 0) isEmpty = false
            })
        })
        return isEmpty
    }
    arr2Map(mapArr) {
        if (!mapArr) return
        mapArr.forEach(item => {
            this.data[item.x][item.y] = item.img
        })
    }
    toString() {
        let arr = [[], [], [], [], [], [], [], []]
    }
    draw() {
        this.clear()
        drawBg()
        this.data.forEach((column, columnIndex) => {
            column.forEach((row, rowIndex) => {
                if (!row.type) return
                const image = new Image()
                const src = '../imgs/' + row.type + '.png'
                image.src = src
                image.onload = () => {
                    const dx = rowIndex * BlockSize + this.offset
                    const dy = columnIndex * BlockSize + this.offset
                    ctx.drawImage(image, dx, dy, BlockSize, BlockSize)
                }
            })
        })
    }
    clear() {
        ctx.clearRect(0, 0, CanvasWidth, CanvasHeight)
    }
}

// 底部缓冲队列
class Queue {
    constructor(length) {
        this.length = length
        this.data = []
    }
    push(item) {
        this.data.push(item)
    }
    processQueueData() {
        const keyMap = new Map()
        this.data.forEach(({ source }) => {
            if (keyMap.has(source.type)) keyMap.set(source.type, keyMap.get(source.type) + 1)
            else keyMap.set(source.type, 1)
        })
        for (const key of keyMap.keys()) {
            if (keyMap.get(key) >= 3) {
                const reset = this.data.filter(item => item.source.type != key)
                this.data.splice(0, 7)
                this.data.push(...reset)
            }
        }
    }
    isFull() {
        this.processQueueData()
        this.updateDom()
        return this.data.length == this.length
    }
    // 更新队列
    updateDom() {
        queueBox.innerHTML = ''
        for (let obj of this.data) {
            const img = document.createElement('img')
            img.src = '../imgs/' + obj.source.type + '.png'
            img.style.height = '50px'
            queueBox.appendChild(img)
        }
    }
}

// 随机数
function randomNum(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min)
}

/**
 *
 * @param {*} xSize 横坐标跨度
 * @param {*} ySize 纵坐标跨度
 * @param {*} picCount 图片总数
 * @param {*} count 每张图片生成次数
 * @returns
 */
function randomMapData(xSize, ySize, picCount, count) {
    if (!count) count = 3
    if (count % 3 !== 0) {
        console.log('count必须是三的倍数')
    }
    let arr = []
    const positionSet = new Set()
    // 每张图
    for (let i = 0; i < picCount; i++) {
        // 每张图三个图标
        for (let j = 0; j < count; j++) {
            const x = randomNum(0, xSize - 1)
            const y = randomNum(0, ySize - 1)
            if (positionSet.has(x + ',' + y)) {
                j--
            } else {
                positionSet.add(x + ',' + y)
                arr.push({
                    x: x,
                    y: y,
                    img: i
                })
            }
        }
    }
    return arr
}
